package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.Photo;
import com.oa.admin.service.IPhotoService;
import com.oa.admin.vo.PhotoListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/photo")
@Api(description = "相片表模块")
public class PhotoController extends BaseController {

    private final IPhotoService photoService;

    @Autowired
    public PhotoController(IPhotoService photoService) {
        this.photoService = photoService;
    }


    @GetMapping("/list/{photoAlbum}")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp findListByAlbum(@ApiParam("相册ID") @PathVariable String photoAlbum){
        List<PhotoListVO> listVOS = new ArrayList<>();
        List<Photo> photo = photoService.selectList(new EntityWrapper<Photo>().eq("photo_album", photoAlbum));
        photo.forEach(d ->{
            PhotoListVO photoListVO = new PhotoListVO();
            BeanUtils.copyProperties(d, photoListVO);
            listVOS.add(photoListVO);
        });
        return new Resp().success(listVOS);
    }

    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@RequestBody List<Photo> photos){
        if(photoService.insertBatch(photos)){
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @DeleteMapping("/{id}")
    @CommonController
    @Description("删除")
    @ApiOperation(value = "删除")
    public Resp del(@PathVariable String id){
        if(photoService.deleteById(id)){
            return new Resp().success();
        }
        return new Resp().failure();
    }
}
